package com.example.myapplication;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Button;

public class ItemListAdapter extends BaseAdapter {
        public caa caa;
    private List<BookBean> objects = new ArrayList<>();

    private Context context;
    private LayoutInflater layoutInflater;

    public ItemListAdapter(Context context,List<BookBean> objects,caa caa) {
        this.caa=caa;
        this.objects=objects;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public Object getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_list, null);
            holder=new ViewHolder(convertView);
            convertView.setTag(new ViewHolder(convertView));
        }else {
            holder=(ViewHolder)convertView.getTag();
        }
            BookBean bookBean=objects.get(position);
        holder.name.setText(bookBean.getName());
        holder.author.setText(bookBean.getAuthor());
        holder.number.setText(bookBean.getNumber()+"");
        holder.btndelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                caa.change(position);

            }
        });
        return convertView;
    }

    protected class ViewHolder {
        private TextView name;
    private TextView author;
    private TextView number;
    private Button btnUpade;
    private Button btndelect;

        public ViewHolder(View view) {
            name = (TextView) view.findViewById(R.id.name);
            author = (TextView) view.findViewById(R.id.author);
            number = (TextView) view.findViewById(R.id.number);
            btnUpade = (Button) view.findViewById(R.id.btn_upade);
            btndelect = (Button) view.findViewById(R.id.btn_delect);
        }
    }
   public interface caa{
        void change( int p);
   }
}
